/**
 * 
 */
package com.hcmu.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import vanhoc.android.utils.ResourceUtils;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.widget.Toast;

import com.telpoo.truyencuoi.R;
import com.hcmu.dto.DatabaseHelper;

/**
 * @author SEVEN
 *
 */
public class StoryDetailModel {
	private Context _context;
	private DatabaseHelper _dbHelper;
	ResourceUtils resUtils;
	
	public StoryDetailModel(Context context) {
		this._context = context;
		this._dbHelper = new DatabaseHelper(context);
		resUtils = new ResourceUtils(_context);
	}
	
	//Lay ve tieu de cua truyen
	public String getStoryTitle(int story_id) {
		_dbHelper.openToRead();
		Cursor cursor =_dbHelper.getStoryTitle(story_id);
		cursor.moveToFirst();
		String title = cursor.getString(0);
		_dbHelper.close();
		return title;
	}
	
	//Lay ve tieu de cua truyen
	public HashMap<String, String> getStoryInfo(int story_id) {
		HashMap<String, String> storyInfo = new HashMap<String, String>();
		
		_dbHelper.openToRead();
		Cursor cursor =_dbHelper.getStoryData(story_id);
		cursor.moveToFirst();
		storyInfo.put("TITLE", cursor.getString(1));
		storyInfo.put("CATEGORY", _dbHelper.getCategoryTitle(Integer.parseInt(cursor.getString(3))));
		storyInfo.put("NUMREAD", cursor.getString(5));
		storyInfo.put("NUMLIKE", cursor.getString(6));
		storyInfo.put("NUMSHARE", cursor.getString(7));
		
		_dbHelper.close();
		return storyInfo;
	}
		
	
	//Lay ve tieu de cua truyen
	public String getStoryContent(int story_id) {
		_dbHelper.openToRead();
		Cursor cursor = _dbHelper.getStoryContent(story_id);
		cursor.moveToFirst();
		String content = cursor.getString(0);
		_dbHelper.close();
		return content;
	}
	
	public void updateNumRead(int id){
		_dbHelper.openToWrite();
		_dbHelper.updateNumRead(id);
		_dbHelper.close();
	}
	
	public void updateNumLike(int id){
		_dbHelper.openToWrite();
		_dbHelper.updateNumLike(id);
		_dbHelper.close();
	}
	
	public Boolean isFavourite(int id){
		_dbHelper.openToRead();
		Cursor cursor = _dbHelper.getFavouriteState(id);
		cursor.moveToFirst();
		int result =  Integer.parseInt(cursor.getString(0));
		_dbHelper.close();
		
		return result==1 ? true : false;
	}
	
	/*public void updateFavourite(int id){
		_dbHelper.openToWrite();
		if (isFavourite(id)) {
			_dbHelper.updateFavouriteState(id, false);
		}else{
			_dbHelper.updateFavouriteState(id, true);
		}
		_dbHelper.close();
	}*/

	public void updateFavourite(int id,Boolean state){
		_dbHelper.openToWrite();
		_dbHelper.updateFavouriteState(id, state);
		_dbHelper.close();
	}
	
	
	//Lay ve danh sach cac id cua cac truyen co cung type voi truyen dang doc
	public List<Integer> getStoryIds(int sid) {
		List<Integer> list = new ArrayList<Integer>();
		_dbHelper.openToRead();
		int type_id = _dbHelper.getCategoryId(sid);
		Cursor cursor = _dbHelper.getIds(type_id);
		cursor.moveToFirst();
		for (int i = 0; i < cursor.getCount(); i++) {
			int id = Integer.parseInt(cursor.getString(0));
			list.add(id);
			
			cursor.moveToNext();
		}
		_dbHelper.close();
		return list;
	}
	
	
	//Lay ve danh sach cac id cua cac truyen co cung type voi truyen dang doc
	public List<Integer> getFavouriteStoryIds() {
		List<Integer> list = new ArrayList<Integer>();
		_dbHelper.openToRead();
		Cursor cursor = _dbHelper.getFavouriteIds();
		cursor.moveToFirst();
		for (int i = 0; i < cursor.getCount(); i++) {
			int id = Integer.parseInt(cursor.getString(0));
			list.add(id);
			cursor.moveToNext();
		}
		_dbHelper.close();
		return list;
	}
		
	//Lay ve danh sach cac id cua cac truyen co cung type voi truyen dang doc
	public Boolean deleteStory(int sid){
		_dbHelper.openToWrite();
		int result = _dbHelper.deleteStory(sid);
		// result = 1 if delete success. else result = 0;
		_dbHelper.close();
		return (result==1) ? true : false;
	}
}
